# 导包
import unittest

import app
from api.IHRMlogin import LoginAPI
from utils import commen_assert

# 创建测试类


class TestLogin(unittest.TestCase):
    # 前置处理
    def setUp(self):
        self.login_api = LoginAPI()

    # 定义测试方法
    # case001_登录成功
    def test01_case001(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "13800000002", "password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, True, 10000, "操作成功")

        # 提取token信息
        app.TOKEN = "Bearer " + response.json().get("data")
        print(app.TOKEN)
        app.headers_data["Authorization"] = app.TOKEN
        print(app.headers_data["Authorization"])
        print(app.headers_data)

    # case002_不输入手机号_登录失败
    def test02_case002(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "", "password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case003_不输入密码_登录失败
    def test03_case003(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "13800000002", "password": ""})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case004_手机号长度小于11位_登录失败
    def test04_case004(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "1380000000", "password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case005_手机号长度大于11位_登录失败
    def test05_case005(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "138000000024", "password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case006_手机号非数字_登录失败
    def test06_case006(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "error", "password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case007_未注册的手机号_登录失败
    def test07_case007(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "13811111112", "password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case008_登录成功_多参
    def test08_case008(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "13800000002", "password": "123456", "parameter":"hahaha"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, True, 10000, "操作成功")

    # case009_少参——手机号_登录失败
    def test09_case009(self):
        # 调用登录接口登录
        response = self.login_api.login({"password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case010_少参——密码_登录失败
    def test10_case010(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "13800000002"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case011_无参_登录失败
    def test11_case011(self):
        # 调用登录接口登录
        response = self.login_api.login(None)
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 99999, "抱歉，系统繁忙，请稍后重试")

    # case012_手机号参数拼写错误_登录失败
    def test12_case012(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobiel": "13800000002", "password": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case013_密码参数拼写错误_登录失败
    def test13_case013(self):
        # 调用登录接口登录
        response = self.login_api.login({"mobile": "13800000002", "passwd": "123456"})
        print(response.json())

        # 断言
        commen_assert(self, response, 200, False, 20001, "用户名或密码错误")